import Common from '../../../common/common.js';
let common = new Common;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //统计月份
    month: '',
    //对账总体情况
    summary: {},
    //对账单列表
    reconciliationList: [],
    //上拉加载控制
    scrollHeight: 0,
    isLoading: false,
    hasData: true,
    pageNum: 1,
    pageSize: 10,
    //月份选择
    month: '',
    monthFormat: ''
  },
  /**
   * 选择月份
   */
  switchMonth: function(e){
    var month = e.detail.value;
    var monthFormat = month.substring(0,4) + '年' + month.substring(5, 7) + '月';
    this.setData({
      month: month,
      monthFormat: monthFormat
    })
    this.resetDatas();
    this.getSummary();
    this.getHistoryList();
  },
  /**
   *  获取对账单统计信息
   */
  getSummary: function () {
    var that = this;
    var data = {
      "orgSeq": getApp().globalData.orgSeq,
      "month": that.data.month
    };
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    common.requestAjax('/api/pay-service/v1/payTrade/queryConfirmedAllRecordInfo', 'POST', data).then(res => {
      wx.hideLoading();
      if (res.data.code == 1) {
        var summary = res.data.result;
        //金额为两位小数
        summary.receivedMoney = summary.receivedMoney.toFixed(2);
        summary.totalMoney = summary.totalMoney.toFixed(2);
        summary.discountMoney = summary.discountMoney.toFixed(2);
        this.setData({
          summary: summary
        })
      } else {
        common.showToast(res.data.message);
      }
    }).catch(err => {
      common.showToast(err);
    })
  },
  /**
   *  获取历史对账单列表
   */
  getHistoryList: function () {
    var that = this;
    var data = {
      "size": that.data.pageSize,
      "current": that.data.pageNum,
      "condition": {
        "orgSeq": getApp().globalData.orgSeq,
        "month": that.data.month
      }
    };
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    common.requestAjax('/api/pay-service/v1/payTrade/queryConfirmedDayRecordListByPage', 'POST', data).then(res => {
      wx.hideLoading();      
      var records = res.data.records;
      //金额为两位小数
      for (var i = 0; i < records.length; i++) {
        records[i].receivedMoney = records[i].receivedMoney.toFixed(2);
        records[i].totalMoney = records[i].totalMoney.toFixed(2);
        records[i].discountMoney = records[i].discountMoney.toFixed(2);
      }
      if (res.data.code == 1) {
        that.setData({
          reconciliationList: that.data.reconciliationList.concat(records)
        })
      } else {
        common.showToast(res.data.message);
      }
      //上拉加载控制
      if (res.data.records == null || res.data.records.length == 0) {
        that.errorDatas();
      } else {
        that.successDatas();
      }
    }).catch(err => {
      console.log(err);
      common.showToast(err);
    })
  },
  /**
   * 上拉加载重置数据
   */
  resetDatas: function () {
    //标识设置加载完成，页数+1，数据清空
    this.setData({
      isLoading: false,
      pageNum: 1
    })
    this.data.reconciliationList = [];
    //隐藏导航栏loading
    wx.hideNavigationBarLoading();
  },
  /**
   * 上拉加载成功处理
   */
  successDatas: function () {
    var that = this;
    //标识设置加载完成，页数+1
    that.setData({
      isLoading: false,
      pageNum: that.data.pageNum + 1
    })
    //隐藏导航栏loading
    wx.hideNavigationBarLoading();
    //隐藏暂无数据
    that.setData({
      hasData: true
    })
  },
  /**
   * 上拉加载失败处理
   */
  errorDatas: function () {
    var that = this;
    //标识设置加载完成
    that.setData({
      isLoading: false
    })
    //隐藏导航栏loading
    wx.hideNavigationBarLoading();
    //显示暂无数据
    if (that.data.pageNum == 1) {
      that.setData({
        hasData: false
      })
    }
  },
  /**
   * 查看详情
   */
  detail: function (e) {
    var item = e.currentTarget.dataset.item;
    item.status = 3;
    wx.navigateTo({
      url: '../reconciliation/reconciliationDetail?item=' + JSON.stringify(item)
    })
  },
  /**
   * 上拉加载事件
   */
  scrollTolower: function (e) {
    var that = this;
    //正在加载中阻止再次查询
    if (that.data.isLoading) {
      return;
    }
    //显示导航栏loading
    wx.showNavigationBarLoading();
    //设置标识加载中
    that.setData({
      isLoading: true
    })
    //获取数据
    that.getHistoryList();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var date = new Date();
    //当前月份
    var fullYear = date.getFullYear();
    var month = date.getMonth();
    month = month + 1;
    if(month < 10){
      month = fullYear + '-0' + month;
    } else {
      month = fullYear + '-' + month;
    }
    var monthFormat = month.substring(0, 4) + '年' + month.substring(5, 7) + '月';
    this.setData({
      month: month,
      monthFormat: monthFormat
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // getApp().globalData.orgSeq = "2e8f4c46-ee3b-4b4b-9270-cf00c28271af";
    //获取对账单统计信息`1
    this.getSummary();
    //获取历史对账单列表
    this.getHistoryList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    //正在加载中阻止再次查询
    if (that.data.isLoading) {
      return;
    }
    //显示导航栏loading
    wx.showNavigationBarLoading();
    //设置标识加载中
    that.setData({
      isLoading: true
    })
    //获取数据
    that.resetDatas();
    that.getHistoryList();
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 10)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    //正在加载中阻止再次查询
    if (that.data.isLoading) {
      return;
    }
    //显示导航栏loading
    wx.showNavigationBarLoading();
    //设置标识加载中
    that.setData({
      isLoading: true
    })
    //获取数据
    that.getHistoryList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})